package study.cv.opencv.task;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import study.cv.opencv.support.utils.WinUtils;

import java.util.List;

@Slf4j
public class Ans_Task {

    private String inputFile = "d:/image/ans/test_01.png";

    @Before
    public void init() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    @Test
    public void test() {
        //
        Mat src = Imgcodecs.imread(inputFile);
        //Windows.imShow("原始图片像", src);
        //
        Mat copy = src.clone();

        //------ 预处理 ------
        Imgproc.cvtColor(copy, copy, Imgproc.COLOR_BGR2GRAY);
        //Windows.imShow("灰度", copy);
        Imgproc.GaussianBlur(copy, copy, new Size(5, 5), 0);
        //Windows.imShow("高斯滤波", copy);
        Imgproc.Canny(copy, copy, 75, 200);
        WinUtils.imShow("边缘检测", copy);

        Imgproc.threshold(copy, copy, 0, 255, Imgproc.THRESH_BINARY_INV|Imgproc.THRESH_OTSU);
        WinUtils.imShow("边缘检测11111111111", copy);
        //------ 轮廓检测 ------
        List<MatOfPoint> contourLt = Lists.newArrayList();
        Mat hierarchy = new Mat();
        Imgproc.findContours(copy, contourLt, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        //检测结果
        System.out.println("fffffff  " + contourLt.size());
        MatOfPoint myContour = contourLt.get(0);
        //Windows.imShow(myContour);
        //原图绘制
//        for (MatOfPoint contour : contourLt) {
//            Imgproc.drawContours(src, Lists.newArrayList(contour), -1, new Scalar(0, 0, 255), 3);
//            Windows.imShow(src);
//        }
        Imgproc.drawContours(src, Lists.newArrayList(myContour), -1, new Scalar(0, 0, 255), 3);
        WinUtils.imShow(src);

        //------ 确保检测到 ------
        //------ 透视变换 ------
    }
}
